package com.example.spring_data_jpa.repository;

import com.example.spring_data_jpa.entity.User;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Modifying;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.query.Param;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * @Auther: 杨通通
 * @Date: 2024/9/9 23:31
 * @Description:
 */
public interface UserRepository extends JpaRepository<User,Integer> {

    @Query(value = "select u from User u where u.city = :city")
    List<User> findUser(@Param("city") String city);

    @Modifying
    @Transactional
    @Query("update User u set u.name = :name where u.id = :id")
    Integer updateUser(@Param("id") Integer id,@Param("name") String name);

    /**
     * 使用User对象作为参数
     * @param user
     * @return
     */
    @Query(value = "select u from User u where u.city = :#{#user.city}")
    List<User> findUserByObj(@Param("user") User user);
    @Query(value = "select u from User u where u.city like %:#{#user.city}%")
    List<User> findUserByObjUserLike(@Param("user") User user);

    @Query(value = "select u from User u where u.name like %:name%")
    Page<User> findUserByNameWithPage(Pageable pageable,@Param("name") String name);
}